/*     */ package com.zimbra.cs.filter;
/*     */ 
/*     */ import com.google.common.base.Joiner;
/*     */ import com.google.common.base.Strings;
/*     */ import com.zimbra.common.filter.Sieve.AddressPart;
/*     */ import com.zimbra.common.filter.Sieve.Condition;
/*     */ import com.zimbra.common.filter.Sieve.DateComparison;
/*     */ import com.zimbra.common.filter.Sieve.Flag;
/*     */ import com.zimbra.common.filter.Sieve.NumberComparison;
/*     */ import com.zimbra.common.filter.Sieve.StringComparison;
/*     */ import com.zimbra.soap.mail.type.FilterAction;
/*     */ import com.zimbra.soap.mail.type.FilterAction.KeepAction;
/*     */ import com.zimbra.soap.mail.type.FilterAction.NotifyAction;
/*     */ import com.zimbra.soap.mail.type.FilterAction.StopAction;
/*     */ import com.zimbra.soap.mail.type.FilterRule;
/*     */ import com.zimbra.soap.mail.type.FilterTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.AddressTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.BodyTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.CommunityContentTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.ConversationTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.CurrentDayOfWeekTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.CurrentTimeTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.DateTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.FlaggedTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.HeaderExistsTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.HeaderTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.Importance;
/*     */ import com.zimbra.soap.mail.type.FilterTest.InviteTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.ListTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.MimeHeaderTest;
/*     */ import com.zimbra.soap.mail.type.FilterTest.SizeTest;
/*     */ import com.zimbra.soap.mail.type.FilterTests;
/*     */ import com.zimbra.soap.mail.type.NestedRule;
/*     */ import java.util.Date;
/*     */ import java.util.List;
/*     */ import org.apache.jsieve.parser.generated.Node;
/*     */ 
/*     */ public final class SieveToSoap extends SieveVisitor
/*     */ {
/*  40 */   private final List<FilterRule> rules = com.google.common.collect.Lists.newArrayList();
/*     */   private final List<String> ruleNames;
/*     */   private FilterRule currentRule;
/*  43 */   private int currentRuleIndex = 0;
/*  44 */   private int nunOfIfProcessingStarted = 0;
/*  45 */   private int numOfIfProcessingDone = 0;
/*     */   private NestedRule currentNestedRule;
/*     */   
/*     */   public SieveToSoap(List<String> ruleNames) {
/*  49 */     this.ruleNames = ruleNames;
/*     */   }
/*     */   
/*     */   public List<FilterRule> toFilterRules() {
/*  53 */     return this.rules;
/*     */   }
/*     */   
/*     */   private String getCurrentRuleName() {
/*  57 */     if ((this.ruleNames == null) || (this.currentRuleIndex >= this.ruleNames.size())) {
/*  58 */       return null;
/*     */     }
/*  60 */     return (String)this.ruleNames.get(this.currentRuleIndex);
/*     */   }
/*     */   
/*     */   protected void visitRule(Node ruleNode, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props)
/*     */   {
/*  65 */     if (phase == SieveVisitor.VisitPhase.end) {
/*  66 */       return;
/*     */     }
/*     */     
/*  69 */     if (!isNestedRule()) {
/*  70 */       this.currentRule = new FilterRule(getCurrentRuleName(), props.isEnabled);
/*  71 */       this.currentRule.setFilterTests(new FilterTests(props.condition.toString()));
/*  72 */       this.rules.add(this.currentRule);
/*  73 */       this.currentRuleIndex += 1;
/*     */       
/*  75 */       this.currentNestedRule = null;
/*     */     }
/*     */     else {
/*  78 */       NestedRule nestedRule = new NestedRule(new FilterTests(props.condition.toString()));
/*  79 */       if (this.currentNestedRule != null)
/*     */       {
/*  81 */         this.currentNestedRule.setChild(nestedRule);
/*     */       }
/*     */       else {
/*  84 */         this.currentRule.setChild(nestedRule);
/*     */       }
/*  86 */       this.currentNestedRule = nestedRule;
/*     */     }
/*     */   }
/*     */   
/*     */   protected void visitIfControl(Node ruleNode, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props)
/*     */   {
/*  92 */     if (phase == SieveVisitor.VisitPhase.end) {
/*  93 */       this.numOfIfProcessingDone += 1;
/*  94 */       return;
/*     */     }
/*  96 */     this.nunOfIfProcessingStarted += 1;
/*     */   }
/*     */   
/*     */   private boolean isNestedRule()
/*     */   {
/* 101 */     if (this.nunOfIfProcessingStarted == this.numOfIfProcessingDone + 1) {
/* 102 */       return false;
/*     */     }
/* 104 */     return true;
/*     */   }
/*     */   
/*     */   private <T extends FilterTest> T addTest(T test, SieveVisitor.RuleProperties props) { FilterTests tests;
/*     */     FilterTests tests;
/* 109 */     if (this.currentNestedRule != null) {
/* 110 */       tests = this.currentNestedRule.getFilterTests();
/*     */     } else {
/* 112 */       tests = this.currentRule.getFilterTests();
/*     */     }
/* 114 */     test.setIndex(tests.size());
/* 115 */     tests.addTest(test);
/* 116 */     if (props.isNegativeTest) {
/* 117 */       test.setNegative(Boolean.valueOf(true));
/*     */     }
/* 119 */     return test;
/*     */   }
/*     */   
/*     */   private <T extends FilterAction> T addAction(T action) {
/* 123 */     if (this.currentNestedRule != null) {
/* 124 */       action.setIndex(this.currentNestedRule.getActionCount());
/* 125 */       this.currentNestedRule.addFilterAction(action);
/*     */     } else {
/* 127 */       action.setIndex(this.currentRule.getActionCount());
/* 128 */       this.currentRule.addFilterAction(action);
/*     */     }
/* 130 */     return action;
/*     */   }
/*     */   
/*     */   protected void visitAttachmentTest(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props)
/*     */   {
/* 135 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 136 */       addTest(new com.zimbra.soap.mail.type.FilterTest.AttachmentTest(), props);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   protected void visitBodyTest(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props, boolean caseSensitive, String value)
/*     */   {
/* 143 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 144 */       FilterTest.BodyTest test = (FilterTest.BodyTest)addTest(new FilterTest.BodyTest(), props);
/* 145 */       if (caseSensitive) {
/* 146 */         test.setCaseSensitive(Boolean.valueOf(true));
/*     */       }
/* 148 */       test.setValue(value);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   protected void visitDateTest(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props, Sieve.DateComparison comparison, Date date)
/*     */   {
/* 155 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 156 */       FilterTest.DateTest test = (FilterTest.DateTest)addTest(new FilterTest.DateTest(), props);
/* 157 */       test.setDateComparison(comparison.toString());
/* 158 */       test.setDate(Long.valueOf(date.getTime() / 1000L));
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   protected void visitCurrentTimeTest(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props, Sieve.DateComparison comparison, String time)
/*     */   {
/* 165 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 166 */       FilterTest.CurrentTimeTest test = (FilterTest.CurrentTimeTest)addTest(new FilterTest.CurrentTimeTest(), props);
/* 167 */       test.setDateComparison(comparison.toString());
/* 168 */       test.setTime(time);
/*     */     }
/*     */   }
/*     */   
/*     */   protected void visitCurrentDayOfWeekTest(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props, List<String> days)
/*     */   {
/* 174 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 175 */       FilterTest.CurrentDayOfWeekTest test = (FilterTest.CurrentDayOfWeekTest)addTest(new FilterTest.CurrentDayOfWeekTest(), props);
/* 176 */       test.setValues(Joiner.on(',').join(days));
/*     */     }
/*     */   }
/*     */   
/*     */   protected void visitTrueTest(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props)
/*     */   {
/* 182 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 183 */       addTest(new com.zimbra.soap.mail.type.FilterTest.TrueTest(), props);
/*     */     }
/*     */   }
/*     */   
/*     */   protected void visitHeaderExistsTest(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props, String header)
/*     */   {
/* 189 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 190 */       addTest(new FilterTest.HeaderExistsTest(header), props);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   protected void visitHeaderTest(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props, List<String> headers, Sieve.StringComparison comparison, boolean caseSensitive, String value)
/*     */   {
/* 197 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 198 */       FilterTest.HeaderTest test = (FilterTest.HeaderTest)addTest(new FilterTest.HeaderTest(), props);
/* 199 */       test.setHeaders(Joiner.on(',').join(headers));
/* 200 */       test.setStringComparison(comparison.toString());
/* 201 */       if (caseSensitive) {
/* 202 */         test.setCaseSensitive(Boolean.valueOf(true));
/*     */       }
/* 204 */       test.setValue(value);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   protected void visitMimeHeaderTest(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props, List<String> headers, Sieve.StringComparison comparison, boolean caseSensitive, String value)
/*     */   {
/* 211 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 212 */       FilterTest.MimeHeaderTest test = (FilterTest.MimeHeaderTest)addTest(new FilterTest.MimeHeaderTest(), props);
/* 213 */       test.setHeaders(Joiner.on(',').join(headers));
/* 214 */       test.setStringComparison(comparison.toString());
/* 215 */       if (caseSensitive) {
/* 216 */         test.setCaseSensitive(Boolean.valueOf(true));
/*     */       }
/* 218 */       test.setValue(value);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   protected void visitAddressTest(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props, List<String> headers, Sieve.AddressPart part, Sieve.StringComparison comparison, boolean caseSensitive, String value)
/*     */   {
/* 225 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 226 */       FilterTest.AddressTest test = (FilterTest.AddressTest)addTest(new FilterTest.AddressTest(), props);
/* 227 */       test.setHeader(Joiner.on(',').join(headers));
/* 228 */       test.setPart(part.toString());
/* 229 */       test.setStringComparison(comparison.toString());
/* 230 */       if (caseSensitive) {
/* 231 */         test.setCaseSensitive(Boolean.valueOf(true));
/*     */       }
/* 233 */       test.setValue(value);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   protected void visitSizeTest(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props, Sieve.NumberComparison comparison, int size, String sizeString)
/*     */   {
/* 240 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 241 */       FilterTest.SizeTest test = (FilterTest.SizeTest)addTest(new FilterTest.SizeTest(), props);
/* 242 */       test.setNumberComparison(comparison.toString());
/* 243 */       test.setSize(sizeString);
/*     */     }
/*     */   }
/*     */   
/*     */   protected void visitAddressBookTest(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props, String header)
/*     */   {
/* 249 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 250 */       addTest(new com.zimbra.soap.mail.type.FilterTest.AddressBookTest(header), props);
/*     */     }
/*     */   }
/*     */   
/*     */   protected void visitContactRankingTest(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props, String header)
/*     */   {
/* 256 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 257 */       addTest(new com.zimbra.soap.mail.type.FilterTest.ContactRankingTest(header), props);
/*     */     }
/*     */   }
/*     */   
/*     */   protected void visitMeTest(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props, String header)
/*     */   {
/* 263 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 264 */       addTest(new com.zimbra.soap.mail.type.FilterTest.MeTest(header), props);
/*     */     }
/*     */   }
/*     */   
/*     */   protected void visitInviteTest(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props, List<String> methods)
/*     */   {
/* 270 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 271 */       FilterTest.InviteTest test = (FilterTest.InviteTest)addTest(new FilterTest.InviteTest(), props);
/* 272 */       test.addMethod(methods);
/*     */     }
/*     */   }
/*     */   
/*     */   protected void visitConversationTest(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props, String where)
/*     */   {
/* 278 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 279 */       FilterTest.ConversationTest test = (FilterTest.ConversationTest)addTest(new FilterTest.ConversationTest(), props);
/* 280 */       test.setWhere(where);
/*     */     }
/*     */   }
/*     */   
/*     */   protected void visitFacebookTest(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props)
/*     */   {
/* 286 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 287 */       addTest(new com.zimbra.soap.mail.type.FilterTest.FacebookTest(), props);
/*     */     }
/*     */   }
/*     */   
/*     */   protected void visitLinkedInTest(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props)
/*     */   {
/* 293 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 294 */       addTest(new com.zimbra.soap.mail.type.FilterTest.LinkedInTest(), props);
/*     */     }
/*     */   }
/*     */   
/*     */   protected void visitSocialcastTest(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props)
/*     */   {
/* 300 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 301 */       addTest(new com.zimbra.soap.mail.type.FilterTest.SocialcastTest(), props);
/*     */     }
/*     */   }
/*     */   
/*     */   protected void visitTwitterTest(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props)
/*     */   {
/* 307 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 308 */       addTest(new com.zimbra.soap.mail.type.FilterTest.TwitterTest(), props);
/*     */     }
/*     */   }
/*     */   
/*     */   protected void visitCommunityRequestsTest(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props)
/*     */   {
/* 314 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 315 */       addTest(new com.zimbra.soap.mail.type.FilterTest.CommunityRequestsTest(), props);
/*     */     }
/*     */   }
/*     */   
/*     */   protected void visitCommunityContentTest(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props) {
/* 320 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 321 */       addTest(new FilterTest.CommunityContentTest(), props);
/*     */     }
/*     */   }
/*     */   
/*     */   protected void visitCommunityConnectionsTest(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props) {
/* 326 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 327 */       addTest(new com.zimbra.soap.mail.type.FilterTest.CommunityConnectionsTest(), props);
/*     */     }
/*     */   }
/*     */   
/*     */   protected void visitListTest(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props)
/*     */   {
/* 333 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 334 */       addTest(new FilterTest.ListTest(), props);
/*     */     }
/*     */   }
/*     */   
/*     */   protected void visitBulkTest(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props)
/*     */   {
/* 340 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 341 */       addTest(new com.zimbra.soap.mail.type.FilterTest.BulkTest(), props);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   protected void visitImportanceTest(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props, FilterTest.Importance importance)
/*     */   {
/* 348 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 349 */       addTest(new com.zimbra.soap.mail.type.FilterTest.ImportanceTest(importance), props);
/*     */     }
/*     */   }
/*     */   
/*     */   protected void visitFlaggedTest(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props, Sieve.Flag flag)
/*     */   {
/* 355 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 356 */       addTest(new FilterTest.FlaggedTest(flag.toString()), props);
/*     */     }
/*     */   }
/*     */   
/*     */   protected void visitDiscardAction(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props)
/*     */   {
/* 362 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 363 */       addAction(new com.zimbra.soap.mail.type.FilterAction.DiscardAction());
/*     */     }
/*     */   }
/*     */   
/*     */   protected void visitFileIntoAction(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props, String folderPath)
/*     */   {
/* 369 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 370 */       addAction(new com.zimbra.soap.mail.type.FilterAction.FileIntoAction(folderPath));
/*     */     }
/*     */   }
/*     */   
/*     */   protected void visitFlagAction(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props, Sieve.Flag flag)
/*     */   {
/* 376 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 377 */       addAction(new com.zimbra.soap.mail.type.FilterAction.FlagAction(flag.toString()));
/*     */     }
/*     */   }
/*     */   
/*     */   protected void visitKeepAction(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props)
/*     */   {
/* 383 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 384 */       addAction(new FilterAction.KeepAction());
/*     */     }
/*     */   }
/*     */   
/*     */   protected void visitRedirectAction(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props, String address)
/*     */   {
/* 390 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 391 */       addAction(new com.zimbra.soap.mail.type.FilterAction.RedirectAction(address));
/*     */     }
/*     */   }
/*     */   
/*     */   protected void visitReplyAction(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props, String bodyTemplate)
/*     */   {
/* 397 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 398 */       addAction(new com.zimbra.soap.mail.type.FilterAction.ReplyAction(bodyTemplate));
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   protected void visitNotifyAction(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props, String emailAddr, String subjectTemplate, String bodyTemplate, int maxBodyBytes, List<String> origHeaders)
/*     */   {
/* 405 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 406 */       FilterAction.NotifyAction action = (FilterAction.NotifyAction)addAction(new FilterAction.NotifyAction());
/* 407 */       action.setAddress(emailAddr);
/* 408 */       if (!Strings.isNullOrEmpty(subjectTemplate)) {
/* 409 */         action.setSubject(subjectTemplate);
/*     */       }
/* 411 */       if (!Strings.isNullOrEmpty(bodyTemplate)) {
/* 412 */         action.setContent(bodyTemplate);
/*     */       }
/* 414 */       if (maxBodyBytes != -1) {
/* 415 */         action.setMaxBodySize(Integer.valueOf(maxBodyBytes));
/*     */       }
/* 417 */       if ((origHeaders != null) && (!origHeaders.isEmpty())) {
/* 418 */         action.setOrigHeaders(Joiner.on(',').join(origHeaders));
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   protected void visitStopAction(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props)
/*     */   {
/* 425 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 426 */       addAction(new FilterAction.StopAction());
/*     */     }
/*     */   }
/*     */   
/*     */   protected void visitTagAction(Node node, SieveVisitor.VisitPhase phase, SieveVisitor.RuleProperties props, String tag)
/*     */   {
/* 432 */     if (phase == SieveVisitor.VisitPhase.begin) {
/* 433 */       addAction(new com.zimbra.soap.mail.type.FilterAction.TagAction(tag));
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/filter/SieveToSoap.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */